package presentation;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import api.IFacade;
import api.IUserDataManager.Gender;
import api.InvalidAuthenticationException;

public class PhaseBook {

	public static IFacade Authenticate(String userName, String password) {

		try {
			Context context = new InitialContext();
			IFacade facadeBean = (IFacade) context
					.lookup("EAI2/IFacade/remote");

			if (facadeBean.authenticate(userName, password))
				return facadeBean;
			else
				return null;
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (InvalidAuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public static Boolean ResetPassword(String userName) {

		try {
			Context context = new InitialContext();
			IFacade facadeBean = (IFacade) context
					.lookup("EAI2/IFacade/remote");

			return facadeBean.resetPassword(userName);
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (InvalidAuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}

	public static Boolean Register(String userName, String password,
			String name, String age, String gender, String locality) {

		try {
			Context context = new InitialContext();
			IFacade facadeBean = (IFacade) context
					.lookup("EAI2/IFacade/remote");

			if (facadeBean.registerUser(userName, password)) {

				facadeBean.authenticate(userName, password);

				int uid = facadeBean.getLoggedUserId();
				facadeBean.updateName(uid, name);

				try {
					int intAge = Integer.parseInt(age);
					facadeBean.updateAge(uid, intAge);
				} catch (NumberFormatException exc) {
					System.out.println("Age is not an Integer");
				}

				if (gender.equals("Male"))
					facadeBean.updateGender(uid, Gender.MALE);
				else if (gender.equals("Female"))
					facadeBean.updateGender(uid, Gender.FEMALE);
				else
					facadeBean.updateGender(uid, null); // rebenta!

				facadeBean.updateCity(uid, locality);

				return true;
			}

			return false;

		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (InvalidAuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}

	}

	public static Boolean UpdateProfile(IFacade facadeBean, String name,
			String age, String locality, String gender) {

		try {
			int uid = facadeBean.getLoggedUserId();
			facadeBean.updateName(uid, name);

			try {
				int intAge = Integer.parseInt(age);
				facadeBean.updateAge(uid, intAge);
			} catch (NumberFormatException exc) {
				System.out.println("Age is not an Integer");
			}

			facadeBean.updateCity(uid, locality);

			if (gender.equals("Male"))
				facadeBean.updateGender(uid, Gender.MALE);
			else if (gender.equals("Female"))
				facadeBean.updateGender(uid, Gender.FEMALE);
			else
				facadeBean.updateGender(uid, null);

			return true;

		} catch (InvalidAuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

}
